﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

namespace WinFormRapor
{
    [Serializable]
    public class Config : ISerializable
    {
        List<Job> _jobs = new List<Job>();
        private static Config _instance = null;
        private Config() { }
        public static Config Get
        {
            get
            {
                if (_instance == null)
                {
                    if (File.Exists(Application.StartupPath + @"\Config.ini"))
                    {
                        FileStream fs = new FileStream(Application.StartupPath + @"\Config.ini", FileMode.Open);
                        BinaryFormatter bnf = new BinaryFormatter();
                        _instance = (Config)bnf.Deserialize(fs);
                        fs.Close();
                    }
                    else
                    {
                        _instance = new Config();
                    }
                }
                return _instance;
            }
        }


        public List<Job> Jobs { get { return _jobs; } }
        private string _server;
        private string _DbName;
        private string _User;
        private string _Pass;
        private string _FirmaKod;

        public string FirmaKod
        {
            get { return _FirmaKod; }
            set { _FirmaKod = value; }
        }
        public string Pass
        {
            get { return _Pass; }
            set { _Pass = value; }
        }
        public string User
        {
            get { return _User; }
            set { _User = value; }
        }
        public string DbName
        {
            get { return _DbName; }
            set { _DbName = value; }
        }
        public string Server
        {
            get { return _server; }
            set { _server = value; }
        }

        public Config(string server, string dbName, string user, string pass, string firmaKod)
        {
            Server = server;
            DbName = dbName;
            User = user;
            Pass = pass;
            FirmaKod = firmaKod;
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Server", _server);
            info.AddValue("DbName", _DbName);
            info.AddValue("User", _User);
            info.AddValue("Pass", _Pass);
            info.AddValue("FirmaKod", _FirmaKod);
        }

        public Config(SerializationInfo info, StreamingContext context)
        {
            Server = info.GetString("Server");
            DbName = info.GetString("DbName");
            User = info.GetString("User");
            Pass = info.GetString("Pass");
            FirmaKod = info.GetString("FirmaKod");
        }
    }
}
